/************************************
 * 描述：本文件主要用于存放通用的逻辑实现
 * 公司：我们公司
 * 时间：2022-02-22
 ************************************/
#include "commons.hxx"

void info_syslog(const char* format, ...) {
	va_list args;
	char message[MAX_MESSAGE_SIZE] = { '\0' };
	va_start(args, format);
	vsprintf(message, format, args);
	va_end(args);

	TC_write_syslog("%s", message);
	TC_printf("%s", message);
}

void show_welcome() {
	info_syslog(ROW_SEPARATOR);
	info_syslog("*Welcome to use '%s.dll(.so)'\n", EX_STRING(SITE));
	info_syslog("*Version: %s\n", EX_STRING(VER));
	info_syslog("*Release time: %s, %s\n", __DATE__, __TIME__);
}

int get_task_attachments(tag_t task, int* count, tag_t** attachments) {
	BEGIN;
	tag_t job = NULLTAG;
	tag_t root_task = NULLTAG;
	CHECK(EPM_ask_job(task, &job));
	CHECK(EPM_ask_root_task(job, &root_task));
	CHECK(EPM_ask_attachments(root_task, EPM_target_attachment, count, attachments));
	END;
}

int foreach_task_attachments(tag_t task, EPM_action_t action, OUR_object_func func) {
	BEGIN;
	int count = 0;
	tag_t* attachments = NULL;
	if (NULL_TAG == task || action > EPM_perform_action || NULL == func)
		return (ifail);
	if (ITK_ok != (ifail = get_task_attachments(task, &count, &attachments)))
		return (ifail);
	info_syslog("*Attachments: %d\n", count);
	for (int i = 0; i < count; i++) {
		func(attachments[i]);
	}
	END;
}

logical is_type_of(tag_t object, const char* parent_type_name) {
	BEGIN;
	logical answer = false;
	tag_t type = NULLTAG;
	CHECK(TCTYPE_ask_object_type(object, &type));
	CHECK(TCTYPE_is_type_of_as_str(type, parent_type_name, &answer));
	return (answer);
}

logical is_contains(tag_t* objects, int count, tag_t object) {
	int i = 0;
	for (i = 0; i < count; i++)
		if (objects[i] == object)
			return (true);
	return (false);
}
